var prefix = ctx + "module/fireWork/";
var form, table, upload,layedit,editIndex;
$(function () {
    layui.use(['form', 'table', 'upload', 'layedit'], function () {
        layedit = layui.layedit;
        form = layui.form;
        table = layui.table;
        upload = layui.upload;
        form.on('select(typeId)', function (data) {
            typeIdChange(data.value, form)
        });
        //拖拽上传
        upload.render({
            elem: '#test10'
            , url: prefix + 'uploadImg'
            , done: function (res) {
                layer.msg('上传成功');
                layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', res.data.src);
                layui.$('#previewImg').val(res.data.filePath);
            }
        });
        //创建一个编辑器
        layedit.set({
            uploadImage: {
                url: prefix + 'uploadImg'
                , type: '' //默认post
            }
        });

        editIndex = layedit.build('content',{tool: [
                'strong' //加粗
                ,'italic' //斜体
                ,'underline' //下划线
                ,'del' //删除线
                ,'left' //左对齐
                ,'center' //居中对齐
                ,'right' //右对齐
                ,'link' //超链接
                ,'image' //插入图片
            ]});
        $(".grid-column-qrcode").each(function() {
            $(this).click(function() {
                previewImg($(this));
            })
        });
    });
});

$().ready(function() {
    validateRule();
});
function typeIdChange(parentId){
    $.ajax({
        type: "POST",
        url: ctx +"module/eventsType/parentIdChange",
        data: {
            parentId:parentId
        },
        success: function(data){
            if(data.code == 0){
                var eventsTypeList = data.data;
                $("#typeChildId").html("");
                if(eventsTypeList.length > 0){
                    for(var index in eventsTypeList) {
                        var str = "<option value=\""+eventsTypeList[index].id+"\" >"+eventsTypeList[index].typeName+"</option>";
                        $("#typeChildId").append(str);
                    }
                }else{
                    $("#typeChildId").append("<option value=\"\" selected=\"selected\">类型/区域</option>");
                    var str = "<option value=\"\" selected=\"selected\">----无----</option>";
                    $("#typeChildId").append(str);
                }
                form.render("select");
            }
        }
    });
}
$.validator.setDefaults({
    submitHandler : function() {
        update();
    }
});

function previewImg(obj) {
    var img = new Image();
    img.src = $(obj).attr("src");
    var imgHtml = "<img src='" + img.src + "' width='100%' height='100%' style='object-fit:contain' />";
    //弹出层
    layer.open({
        type: 1,
        // shade: true,
        shadeClose : true, // 点击遮罩关闭层
        area:['80%','80%'],
        title: false, //不显示标题
        content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        cancel: function () {

        }
    });
}

function update() {
    if (!$('#handleResult').val()) {
        parent.layer.msg("请回复后在提交!");
    }
    var url = prefix + ($('#signupForm').find("#id").val() == '' ? "save" : "update");
    var content = layedit.getContent(editIndex);
    var data = {};
    $('#signupForm').serializeArray().forEach(function (res) {
        data[res.name] = res.value
    });
    data.content = content;
    $.ajax({
        cache: true,
        type: "POST",
        url: url,
        data: data,
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }
        }
    });
}


function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules : {
            sort : {
                required : true
            }
        },
        messages : {
            sort : {
                required : icon + "请输入索引"
            }
        }
    })
}
